package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ChatConfig;
import com.ruoyi.system.service.IChatConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 聊天记录Controller
 * 
 * @author ruoyi
 * @date 2024-10-12
 */
@RestController
@Api(tags = "聊天记录")
@RequestMapping("/system/chatconfig")
public class ChatConfigController extends BaseController
{
    @Autowired
    private IChatConfigService chatConfigService;

    /**
     * 查询聊天记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChatConfig chatConfig)
    {
        startPage();
        List<ChatConfig> list = chatConfigService.selectChatConfigList(chatConfig);
        return getDataTable(list);
    }

    /**
     * 导出聊天记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:config:export')")
    @Log(title = "聊天记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChatConfig chatConfig)
    {
        List<ChatConfig> list = chatConfigService.selectChatConfigList(chatConfig);
        ExcelUtil<ChatConfig> util = new ExcelUtil<ChatConfig>(ChatConfig.class);
        util.exportExcel(response, list, "聊天记录数据");
    }

    @ApiOperation("聊天记录详情")
    @Anonymous
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        ChatConfig chatConfig = chatConfigService.selectChatConfigByUniqueId(id);
        JsonMapper jsonMapper = new JsonMapper();
        ArrayNode arrayNode = jsonMapper.createArrayNode();
        if (null != chatConfig) {
            String contentA = chatConfig.getContentA();
            if (StringUtils.isNotEmpty(contentA)) {
                String[] split = contentA.split("\n");
                for (int i = 0; i < split.length; i++) {
                    ObjectNode objectNode = jsonMapper.createObjectNode();
                    objectNode.put("nickName", chatConfig.getNickNameA());
                    objectNode.put("content", split[i]);
                    objectNode.put("avatar", chatConfig.getAvatarA());
                    arrayNode.add(objectNode);
                }
            }
            String contentB = chatConfig.getContentB();
            if (StringUtils.isNotEmpty(contentB)) {
                ObjectNode objectNode = jsonMapper.createObjectNode();
                objectNode.put("nickName", chatConfig.getNickNameB());
                objectNode.put("avatar", chatConfig.getAvatarB());
                objectNode.put("content", contentB);
                arrayNode.add(objectNode);
            }
            chatConfig.setFields(arrayNode);
        }
        return success(chatConfig);
    }

    /**
     * 获取聊天记录详细信息
     */
    @ApiOperation("聊天记录详情")
    @PreAuthorize("@ss.hasPermi('system:config:query')")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult getDetailInfo(@PathVariable("id") Long id)
    {
        ChatConfig chatConfig = chatConfigService.selectChatConfigById(id);
        return success(chatConfig);
    }

    /**
     * 新增聊天记录
     */
    @PreAuthorize("@ss.hasPermi('system:config:add')")
    @Log(title = "聊天记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChatConfig chatConfig)
    {
        return toAjax(chatConfigService.insertChatConfig(chatConfig));
    }

    /**
     * 修改聊天记录
     */
    @PreAuthorize("@ss.hasPermi('system:config:edit')")
    @Log(title = "聊天记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChatConfig chatConfig)
    {
        return toAjax(chatConfigService.updateChatConfig(chatConfig));
    }

    /**
     * 删除聊天记录
     */
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "聊天记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(chatConfigService.deleteChatConfigByIds(ids));
    }
}
